package com.loyal.loyalerp.humenresource.kaoqin.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.humenresource.kaoqinMonth.service.KaoqinMonthService;
import com.loyal.loyalerp.model.ChangeInitData;
import com.loyal.loyalerp.model.GyUser;
import com.loyal.loyalerp.model.KaoQinMessage;
import com.loyal.loyalerp.model.RsCheck;
import com.loyal.loyalerp.model.vo.CompanyIdAndYears;
import com.loyal.loyalerp.model.vo.ExcelVo;
import com.loyal.loyalerp.model.vo.KaoQinPageInfo;
import com.loyal.loyalerp.model.vo.Result;
import com.loyal.loyalerp.model.vo.UsernameAndWork;
import com.loyal.loyalerp.service.EmployeeService;
import com.loyal.loyalerp.service.impl.KaoQinServiceImpl;
import com.loyal.loyalerp.utils.ValidateUtil;

@Controller
@RequestMapping("kaoqin")
public class KaoQinController {
	final static String permission = "rs_kaoqin";

	@Autowired
	private KaoQinServiceImpl service;
	@Autowired
	private EmployeeService employService;
	@Autowired
	private KaoqinMonthService kaoqinService;

	// 进入考勤页面
	@RequestMapping("enterKaoQin")
	public String enterKaoQin() {
		return "basedata/kaoqin/kaoqin";
	}

	@RequestMapping("searchjsp")
	public String enterSearch() {
		return "basedata/kaoqin/search";
	}
	
	
	
	//进入查看请假详情
	@RequestMapping("qingjiaDetails")
	public String enterqingjiaDetails() {
		return "basedata/kaoqin/detailQinjia";
	}

	// 进入考勤页面
	@RequestMapping("enterInitData")
	public String enterInitData() {
		return "basedata/kaoqin/selectCompany";
	}

	// 初始化年份
	@RequestMapping("initData")
	public @ResponseBody Result initData(String years, String companyname) {
		Result result = new Result();
		// 判断权限
		if (!ValidateUtil.init(permission)) {
			result.setStatus(1000);
			return result;
		}

		// 判断当前年份有没有被定义过
		int companyid = employService.selectCompanyIdByName(companyname);
		// 判断去年有没有被月结
		CompanyIdAndYears cmd = new CompanyIdAndYears();
		cmd.setCompanyid(companyid);
		cmd.setYears(Integer.parseInt(years) - 1);
		int isColseFlag = kaoqinService.selectLastYearHasEnd(cmd);
		if (isColseFlag == 1) {
			int count = service.selectYearHasInit(Integer.parseInt(years), companyid);
			if (count == 0) {
				// 判断该公司是否有员工
				if (service.hasPerson(companyid) == 0) {
					result.setStatus(600);
				} else {
					try {
						service.initData(years, companyid);
						RsCheck check = new RsCheck();
						check.setYears(Integer.parseInt(years));
						check.setIscheck(0);
						check.setAbandoncheck(0);
						check.setCompanyid(companyid);
						service.insertCompanyCheck(check);
						result.setStatus(200);
					} catch (ParseException e) {
						result.setStatus(500);
					}
				}
			} else {
				result.setStatus(400);
			}
		} else {
			result.setStatus(700);
		}
		return result;
	}

	// 查看所有员工期初其
	@RequestMapping("getAllPersonInit")
	public @ResponseBody KaoQinPageInfo getAllPersonInit(@RequestParam(name = "page", required = false) String page,
			@RequestParam(name = "rows", required = false) String rows,
			@RequestParam(name = "searchOper", required = false) String searchOper,
			@RequestParam(name = "searchField", required = false) String searchField,
			@RequestParam(name = "searchString", required = false) String searchString,
			@RequestParam(name = "_search", required = false) Boolean _search) {
		if (_search == false) {
			if (page.equals("1")) {
				PageHelper.startPage(1, Integer.parseInt(rows));
				List<KaoQinMessage> list = service.selectAllPersonInitData("1");
				PageInfo<KaoQinMessage> info = new PageInfo(list);
				KaoQinPageInfo kaoqin = new KaoQinPageInfo();
				kaoqin.setRows(list);
				kaoqin.setPage(info.getPageNum() + "");
				kaoqin.setTotal(info.getPages() + "");
				kaoqin.setResult("获取员工");
				return kaoqin;
			} else {
				PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
				List<KaoQinMessage> list = service.selectAllPersonInitData("1");
				PageInfo<GyUser> info = new PageInfo(list);
				KaoQinPageInfo kaoqin = new KaoQinPageInfo();
				kaoqin.setRows(list);
				kaoqin.setPage(info.getPageNum() + "");
				kaoqin.setTotal(info.getPages() + "");
				kaoqin.setResult("获取员工");
				return kaoqin;
			}
		}

		else {
			if (searchOper != null) {
				if (searchOper.equals("eq")) {
					if (searchField.equals("username")) {
						int index = searchString.indexOf(";");
						String username = searchString.substring(0, index);
						String isWork = searchString.substring(index + 1, searchString.length());
						UsernameAndWork vo = new UsernameAndWork();
						if (isWork.equals("2")) {
							vo.setUsername(username);
							vo.setIsWork(null);
						} else {
							vo.setUsername(username);
							vo.setIsWork(isWork);
						}
						List<KaoQinMessage> list = service.selectPersonKaoQinByName(vo);
						KaoQinPageInfo employ = new KaoQinPageInfo();
						employ.setRows(list);
						return employ;
					}
					if (searchField.equals("qichu")) {
						int a = searchString.indexOf("|");
						int companyid = employService.selectCompanyIdByName(searchString.substring(0, a));
						int years = Integer.parseInt(searchString.substring(a + 1, searchString.length()));
						CompanyIdAndYears cmd = new CompanyIdAndYears();
						cmd.setCompanyid(companyid);
						cmd.setYears(years);
						List<KaoQinMessage> list = service.selectInitDataByCompanyIdAndYears(cmd);
						KaoQinPageInfo employ = new KaoQinPageInfo();
						employ.setRows(list);
						return employ;
					}
					if (searchField.equals("work")) {
						if (searchString.equals("1")) {
							PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
							List<KaoQinMessage> list = service.selectAllPersonInitData("1");
							PageInfo<KaoQinMessage> info = new PageInfo(list);
							KaoQinPageInfo kaoqin = new KaoQinPageInfo();
							kaoqin.setRows(list);
							kaoqin.setPage(info.getPageNum() + "");
							kaoqin.setTotal(info.getPages() + "");
							return kaoqin;
						} else if (searchString.equals("0")) {
							PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
							List<KaoQinMessage> list = service.selectAllPersonInitData("0");
							PageInfo<KaoQinMessage> info = new PageInfo(list);
							KaoQinPageInfo kaoqin = new KaoQinPageInfo();
							kaoqin.setRows(list);
							kaoqin.setPage(info.getPageNum() + "");
							kaoqin.setTotal(info.getPages() + "");
							return kaoqin;
						} else {
							PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
							List<KaoQinMessage> list = service.selectAllPersonInitData(null);
							PageInfo<KaoQinMessage> info = new PageInfo(list);
							KaoQinPageInfo kaoqin = new KaoQinPageInfo();
							kaoqin.setRows(list);
							kaoqin.setPage(info.getPageNum() + "");
							kaoqin.setTotal(info.getPages() + "");
							return kaoqin;
						}
					}
				}
			}
		}

		return null;
	}

	// 修改期初数据
	@RequestMapping("changeInitData")
	public @ResponseBody Result changeInitData(@RequestBody ChangeInitData[] data) {
		Result result = new Result();
		// 判断权限
		if (!ValidateUtil.validateEdit(permission)) {
			result.setStatus(1000);
			return result;
		}

		int companyid = employService.selectCompanyIdByName(data[0].getCompanyName());
		CompanyIdAndYears cmd = new CompanyIdAndYears();
		cmd.setCompanyid(companyid);
		cmd.setYears(data[0].getKjYear());
		// 判断当前数据有没有审核过
		int isCheck = service.selectIsCheck(cmd);
		if (isCheck == 1) {
			result.setStatus(400);
		} else {
			try {
				for (ChangeInitData ini : data) {
					service.updateInitData(ini);
				}
				result.setStatus(200);
			} catch (Exception e) {
				result.setStatus(500);
			}
		}

		return result;
	}

	// 期初数据审核
	@RequestMapping("check")
	public @ResponseBody Result check(String years, String companyname) {
		Result result = new Result();
		// 判断权限
		if (!ValidateUtil.validateCheck(permission)) {
			result.setStatus(1000);
			return result;
		}
		int companyid = employService.selectCompanyIdByName(companyname);
		CompanyIdAndYears cmd = new CompanyIdAndYears();
		cmd.setCompanyid(companyid);
		cmd.setYears(Integer.parseInt(years));
		// 判断当前年份有没有被定义过
		int count = service.selectYearHasInit(Integer.parseInt(years), companyid);
		if (count != 0) {
			// 判断当前数据有没有审核过
			int isCheck = service.selectIsCheck(cmd);
			if (isCheck == 1) {
				result.setStatus(400);
			} else {
				service.updateIsCheck(cmd);
				result.setStatus(200);
			}

		} else {
			result.setStatus(500);
		}
		return result;
	}

	// 期初数据弃审
	@RequestMapping("abandonCheck")
	public @ResponseBody Result abandonCheck(String years, String companyname) {
		Result result = new Result();
		// 判断权限
		if (!ValidateUtil.abandonCheck(permission)) {
			result.setStatus(1000);
			return result;
		}
		int companyid = employService.selectCompanyIdByName(companyname);
		CompanyIdAndYears cmd = new CompanyIdAndYears();
		cmd.setCompanyid(companyid);
		cmd.setYears(Integer.parseInt(years));
		// 判断当前年份有没有被定义过
		int count = service.selectYearHasInit(Integer.parseInt(years), companyid);
		if (count != 0) {
			// 判断当前数据有没有弃审
			int abandonCheck = service.selectAbandonCheck(cmd);
			if (abandonCheck == 1) {
				result.setStatus(300);
			} else {
				service.updateAbandonCheck(cmd);
				service.updateIsNoCheck(cmd);
				result.setStatus(200);
			}
		} else {
			result.setStatus(500);
		}
		return result;
	}

	// 导出excel表格
	@RequestMapping("exportExcel")
	public String exportExcel(String isWork, HttpServletResponse response,String years,String companyname) throws FileNotFoundException {
		XSSFWorkbook book = new XSSFWorkbook();
		XSSFSheet sheet = book.createSheet();
		XSSFRow row = sheet.createRow(0);
		row.createCell(0).setCellValue("公司名称");
		row.createCell(1).setCellValue("员工姓名");
		row.createCell(2).setCellValue("年份");
		row.createCell(3).setCellValue("上年假期");
		row.createCell(4).setCellValue("本年假期");
		row.createCell(5).setCellValue("上年加班数");
		row.createCell(6).setCellValue("一月份上班");
		row.createCell(7).setCellValue("二月份上班");
		row.createCell(8).setCellValue("三月份上班");
		row.createCell(9).setCellValue("四月份上班");
		row.createCell(10).setCellValue("五月份上班");
		row.createCell(11).setCellValue("六月份上班");
		row.createCell(12).setCellValue("七月份上班");
		row.createCell(13).setCellValue("八月份上班");
		row.createCell(14).setCellValue("九月份上班");
		row.createCell(15).setCellValue("十月份上班");
		row.createCell(16).setCellValue("十一月份上班");
		row.createCell(17).setCellValue("十二份上班");
		row.createCell(18).setCellValue("一月份请假");
		row.createCell(19).setCellValue("二月份请假");
		row.createCell(20).setCellValue("三月份请假");
		row.createCell(21).setCellValue("四月份请假");
		row.createCell(22).setCellValue("五月份请假");
		row.createCell(23).setCellValue("六月份请假");
		row.createCell(24).setCellValue("七月份请假");
		row.createCell(25).setCellValue("八月份请假");
		row.createCell(26).setCellValue("九月份请假");
		row.createCell(27).setCellValue("十月份请假");
		row.createCell(28).setCellValue("十一月份请假");
		row.createCell(29).setCellValue("十二月份请假");
		row.createCell(30).setCellValue("一月份有薪");
		row.createCell(31).setCellValue("二月份有薪");
		row.createCell(32).setCellValue("三月份有薪");
		row.createCell(33).setCellValue("四月份有薪");
		row.createCell(34).setCellValue("五月份有薪");
		row.createCell(35).setCellValue("六月份有薪");
		row.createCell(36).setCellValue("七月份有薪");
		row.createCell(37).setCellValue("八月份有薪");
		row.createCell(38).setCellValue("九月份有薪");
		row.createCell(39).setCellValue("十月份有薪");
		row.createCell(40).setCellValue("十一月份有薪");
		row.createCell(41).setCellValue("十二月份有薪");
		row.createCell(42).setCellValue("上班总数");
		row.createCell(43).setCellValue("请假总数");
		row.createCell(44).setCellValue("带薪总数");
		row.createCell(45).setCellValue("实休年假");
		row.createCell(46).setCellValue("剩余年假");
		row.createCell(47).setCellValue("调休合计");
		row.createCell(48).setCellValue("法定假合计");
		row.createCell(49).setCellValue("病假合计");
		row.createCell(50).setCellValue("事假合计");
		row.createCell(51).setCellValue("一月份kpi");
		row.createCell(52).setCellValue("二月份kpi");
		row.createCell(53).setCellValue("三月份kpi");
		row.createCell(54).setCellValue("四月份kpi");
		row.createCell(55).setCellValue("五月份kpi");
		row.createCell(56).setCellValue("六月份kpi");
		row.createCell(57).setCellValue("七月份kpi");
		row.createCell(58).setCellValue("八月份kpi");
		row.createCell(59).setCellValue("九月份kpi");
		row.createCell(60).setCellValue("十月份kpi");
		row.createCell(61).setCellValue("十一月份kpi");
		row.createCell(62).setCellValue("十二月份kpi");
		row.createCell(63).setCellValue("备注一");
		row.createCell(64).setCellValue("备注二");
		row.createCell(65).setCellValue("备注三");
		List<KaoQinMessage> list = null;
		ExcelVo vo = vo = new ExcelVo();;
		vo.setCompanyname(companyname);
		vo.setYears(years);
		if (isWork.equals("1")) {
			vo.setIswork("1");
			list = service.selectAllPersonInitDataByExcel(vo);
		} else if (isWork.equals("0")) {
			vo.setIswork("0");
			list = service.selectAllPersonInitDataByExcel(vo);
		} else {
			vo.setIswork(null);
			list = service.selectAllPersonInitDataByExcel(vo);
		}
		for (int i = 0; i < list.size(); i++) {
			KaoQinMessage msg = list.get(i);
			System.out.println(msg);
			XSSFRow newRow = sheet.createRow(i + 1);
			newRow.createCell(0).setCellValue(msg.getCompanyName());
			newRow.createCell(1).setCellValue(msg.getPersonName());
			newRow.createCell(2).setCellValue(msg.getKjYear());
			newRow.createCell(3).setCellValue(msg.getHolidaysStart());
			newRow.createCell(4).setCellValue(msg.getHolidays());
			newRow.createCell(5).setCellValue(msg.getOverTimeDaysStart());
			newRow.createCell(6).setCellValue(msg.getWorkDays01());
			newRow.createCell(7).setCellValue(msg.getWorkDays02());
			newRow.createCell(8).setCellValue(msg.getWorkDays03());
			newRow.createCell(9).setCellValue(msg.getWorkDays04());
			newRow.createCell(10).setCellValue(msg.getWorkDays05());
			newRow.createCell(11).setCellValue(msg.getWorkDays06());
			newRow.createCell(12).setCellValue(msg.getWorkDays07());
			newRow.createCell(13).setCellValue(msg.getWorkDays08());
			newRow.createCell(14).setCellValue(msg.getWorkDays09());
			newRow.createCell(15).setCellValue(msg.getWorkDays10());
			newRow.createCell(16).setCellValue(msg.getWorkDays11());
			newRow.createCell(17).setCellValue(msg.getWorkDays12());
			newRow.createCell(18).setCellValue(msg.getOffDays01());
			newRow.createCell(19).setCellValue(msg.getOffDays02());
			newRow.createCell(20).setCellValue(msg.getOffDays03());
			newRow.createCell(21).setCellValue(msg.getOffDays04());
			newRow.createCell(22).setCellValue(msg.getOffDays05());
			newRow.createCell(23).setCellValue(msg.getOffDays06());
			newRow.createCell(24).setCellValue(msg.getOffDays07());
			newRow.createCell(25).setCellValue(msg.getOffDays08());
			newRow.createCell(26).setCellValue(msg.getOffDays09());
			newRow.createCell(27).setCellValue(msg.getOffDays10());
			newRow.createCell(28).setCellValue(msg.getOffDays11());
			newRow.createCell(29).setCellValue(msg.getOffDays12());
			newRow.createCell(30).setCellValue(msg.getSalaryDays01());
			newRow.createCell(31).setCellValue(msg.getSalaryDays02());
			newRow.createCell(32).setCellValue(msg.getSalaryDays03());
			newRow.createCell(33).setCellValue(msg.getSalaryDays04());
			newRow.createCell(34).setCellValue(msg.getSalaryDays05());
			newRow.createCell(35).setCellValue(msg.getSalaryDays06());
			newRow.createCell(36).setCellValue(msg.getSalaryDays07());
			newRow.createCell(37).setCellValue(msg.getSalaryDays08());
			newRow.createCell(38).setCellValue(msg.getSalaryDays09());
			newRow.createCell(39).setCellValue(msg.getSalaryDays10());
			newRow.createCell(40).setCellValue(msg.getSalaryDays11());
			newRow.createCell(41).setCellValue(msg.getSalaryDays12());
			newRow.createCell(42).setCellValue(msg.getWorkDaysTotal());
			newRow.createCell(43).setCellValue(msg.getOffDaysTotal());
			newRow.createCell(44).setCellValue(msg.getSalaryDaysTotal());
			newRow.createCell(45).setCellValue(msg.getHolidaysTotal());
			newRow.createCell(46).setCellValue(msg.getHolidaysEnd());
			newRow.createCell(47).setCellValue(msg.getOverTimeDaysTotal());
			newRow.createCell(48).setCellValue(msg.getLawDaysTotal());
			newRow.createCell(49).setCellValue(msg.getSickDaysTotal());
			newRow.createCell(50).setCellValue(msg.getPersonDaysTotal());
			newRow.createCell(51).setCellValue(msg.getKpi01());
			newRow.createCell(52).setCellValue(msg.getKpi02());
			newRow.createCell(53).setCellValue(msg.getKpi03());
			newRow.createCell(54).setCellValue(msg.getKpi04());
			newRow.createCell(55).setCellValue(msg.getKpi05());
			newRow.createCell(56).setCellValue(msg.getKpi06());
			newRow.createCell(57).setCellValue(msg.getKpi07());
			newRow.createCell(58).setCellValue(msg.getKpi08());
			newRow.createCell(59).setCellValue(msg.getKpi09());
			newRow.createCell(60).setCellValue(msg.getKpi10());
			newRow.createCell(61).setCellValue(msg.getKpi11());
			newRow.createCell(62).setCellValue(msg.getKpi12());
			newRow.createCell(63).setCellValue(msg.getRemark1());
			newRow.createCell(64).setCellValue(msg.getRemark2());
			newRow.createCell(65).setCellValue(msg.getRemark3());
		}
		File file = new File("考勤初始化表.xlsx");
		FileOutputStream fos = new FileOutputStream(file);
		try {
			book.write(fos);
			response.setHeader("Content-disposition",
					"attachment; fileName=" + new String("考勤期初表.xlsx".getBytes("gbk"), "iso8859-1"));
			response.setContentType("application/x-rarx-rar-compressed");
			OutputStream out = response.getOutputStream();
			FileInputStream inputStream = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = inputStream.read(buffer)) != -1) {
				out.write(buffer, 0, i);
			}
			out.flush();
			out.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
